import { ref } from 'vue';
import { useAuthStore } from '@/store/modules/auth';
import { get } from '@/service/api/api';

let topFormModel;
let bottomFormModel;
const moduleNoSearchData = ref(null);
const moduleNoList = ref([]);

function getModuleNoList() {
  get('/rcerp/agv/erp/findAllProdNo', {}).then(({ data, error }) => {
    if (!error) {
      moduleNoList.value = data.map(item => {
        return {
          label: item.moduleNo,
          value: item.moduleNo
        };
      });
    }
  });
}

const contractList = ref([]);

const authStore = useAuthStore();
export default {
  url: '/qms/agv/PackCheck/find',
  addUrl: '/qms/agv/PackCheck/add',
  editUrl: '/qms/agv/PackCheck/update',
  delUrl: '/qms/agv/PackCheck/deleteBatch',
  editMethod: 'put',
  delMethod: 'put',
  searchFormList: [
    {
      type: 'input',
      key: 'moduleNo',
      label: '生产型号',
      placeholder: '请输入生产型号',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 5
    },
    {
      type: 'input',
      key: 'serialNum',
      label: '单据号',
      placeholder: '请输入单据号',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 4
    },
    {
      type: 'select',
      key: 'state',
      label: '审核状态',
      placeholder: '请选择审核状态',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 5,
      selectOptions: [
        {
          value: '4',
          label: '待领班审核'
        },
        {
          value: '1',
          label: '待修改'
        },
        {
          value: '3',
          label: '审核完成'
        },
        {
          value: '2',
          label: '待主管审核'
        },
        {
          value: '0',
          label: '待QA审核'
        }
      ]
    },
    {
      type: 'dateRangePicker',
      key: 'time',
      label: '日期',
      span: 24,
      md: 12,
      lg: 10,
      xl: 8,
      xxl: 6
    }
  ],
  searchParam: ref({
    moduleNo: null,
    state: null,
    time: null
  }),
  column: [
    {
      key: 'dataIndex',
      dataIndex: 'dataIndex',
      title: '序号',
      width: 70,
      fixed: 'left',
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'moduleNo',
      dataIndex: 'moduleNo',
      title: '生产型号',
      width: 150,
      align: 'center',
      fixed: 'left',
      checked: true,
      ellipsis: true
    },
    {
      key: 'serialNum',
      dataIndex: 'serialNum',
      title: '单据号',
      width: 130,
      align: 'center',
      fixed: 'left',
      checked: true,
      ellipsis: true
    },
    {
      key: 'contract',
      dataIndex: 'contract',
      title: '订单PO',
      width: 150,
      fixed: 'left',
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'state',
      dataIndex: 'state',
      title: '状态',
      width: 120,
      align: 'center',
      checked: true,
      ellipsis: true,
      customRender: ({ record }) => {
        return ['待QA审核', '待修改', '待主管审核', '审核完成', '待领班审核'][record.state];
      }
    },
    {
      key: 'qty',
      dataIndex: 'qty',
      title: '订单数量',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'date',
      dataIndex: 'date',
      title: '日期',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'cycle',
      dataIndex: 'cycle',
      title: '周期',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'packNum',
      dataIndex: 'packNum',
      title: '内包包数',
      width: 150,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'innerSerial',
      dataIndex: 'innerSerial',
      title: '内标流水号',
      width: 150,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'boxNum',
      dataIndex: 'boxNum',
      title: '外箱箱数',
      width: 150,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'outerSerial',
      dataIndex: 'outerSerial',
      title: '外标流水号',
      width: 150,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'underrun',
      dataIndex: 'underrun',
      title: '欠数',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    // {
    //   key: 'deliveryQty',
    //   dataIndex: 'deliveryQty',
    //   title: '完成数',
    //   width: 100,
    //   align: 'center',
    //   checked: true,
    //   ellipsis: true
    // },

    {
      key: 'custProdNo',
      dataIndex: 'custProdNo',
      title: '客户型号',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'packs',
      dataIndex: 'packs',
      title: '内包数量',
      width: 150,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'boxs',
      dataIndex: 'boxs',
      title: '外标数量',
      width: 150,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'targetContract',
      dataIndex: 'targetContract',
      title: '终端PO',
      width: 200,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'completeNum',
      dataIndex: 'completeNum',
      title: '打标签数量',
      width: 150,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'remainder',
      dataIndex: 'remainder',
      title: '待打标签数量',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'dateClass',
      dataIndex: 'dateClass',
      title: '班别',
      width: 120,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'operator',
      dataIndex: 'operator',
      title: '首/自主',
      width: 120,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'qa',
      dataIndex: 'qa',
      title: 'IPQC审核',
      width: 120,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'qaTime',
      dataIndex: 'qaTime',
      title: 'QA审核时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'foreman',
      dataIndex: 'foreman',
      title: '领班审核',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'foremanTime',
      dataIndex: 'foremanTime',
      title: '领班审核时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'examiner',
      dataIndex: 'examiner',
      title: '操作员',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'examinerTime',
      dataIndex: 'examinerTime',
      title: '主管审核时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'remark',
      dataIndex: 'remark',
      title: '备注',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'createTime',
      dataIndex: 'createTime',
      title: '创建时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'updateTime',
      dataIndex: 'updateTime',
      title: '修改时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'updateBy',
      dataIndex: 'updateBy',
      title: '修改人',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    }
  ],

  scroll: { x: 2500, y: 'auto' },
  topFormList: [
    // 用于获取model
    {
      type: 'getModel',
      hidden: true,
      handle(topModel, personModle) {
        topFormModel = topModel;
        bottomFormModel = personModle;
      }
    },
    {
      type: 'select',
      key: 'moduleNo',
      label: '生产型号',
      span: 12,
      rule: {
        required: true,
        message: '请输入生产型号',
        trigger: 'change'
      },
      showSelectOtherBtn: true,
      selectOtherBtnText: '查询',
      search: true,
      handleSelectOtherBtnClick: () => {
        get('/rcerp/agv/erp/findOrderDetail', {
          moduleNo: topFormModel.value.moduleNo
        }).then(({ data, error }) => {
          if (!error) {
            contractList.value = data.map(item => {
              return {
                label: item.contract,
                value: item.batchNo,
                ...item
              };
            });
          }
        });
      },
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      selectOptions: moduleNoList
    },

    {
      type: 'select',
      key: 'contract',
      label: '订单PO',
      span: 12,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      selectOptions: contractList,
      change() {
        get('/qms/agv/PackCheck/findByBatchNo', {
          moduleNo: topFormModel.value.moduleNo,
          batchNo: topFormModel.value.contract
        }).then(({ data, error }) => {
          if (!error) {
            Object.keys(topFormModel.value).forEach(item => {
              topFormModel.value[item] = data[item] || topFormModel.value[item];
            });
          }
        });
      }
    },
    {
      type: 'datePicker',
      key: 'date',
      label: '时间',
      span: 8,
      rule: {
        required: true,
        message: '请选择时间',
        trigger: 'change'
      },
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      showTime: true
    },

    {
      type: 'input',
      key: 'mktDate',
      label: '原始交期',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },

    {
      type: 'input',
      key: 'qty',
      label: '订单数量',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },

    {
      type: 'input',
      key: 'cycle',
      label: '周期',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },

    {
      type: 'input',
      key: 'packNum',
      label: '内包包数',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },

    {
      type: 'input',
      key: 'innerSerial',
      label: '内标流水号',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },

    {
      type: 'input',
      key: 'boxNum',
      label: '外箱箱数',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },

    {
      type: 'input',
      key: 'outerSerial',
      label: '外标流水号',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },

    {
      type: 'input',
      key: 'underrun',
      label: '欠数',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },

    {
      type: 'input',
      key: 'deliveryQty',
      label: '完成数',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },

    {
      type: 'input',
      key: 'custProdNo',
      label: '客户型号',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },

    {
      type: 'input',
      key: 'custProdNoAdd',
      label: '物料号',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },

    {
      type: 'input',
      key: 'packs',
      label: '内包数量',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },

    {
      type: 'input',
      key: 'boxs',
      label: '外标数量',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },

    {
      type: 'input',
      key: 'targetContract',
      label: '终端PO',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },

    {
      type: 'input',
      key: 'completeNum',
      label: '打标签数量',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },

    {
      type: 'input',
      key: 'remainder',
      label: '待打标签数量',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },

    {
      type: 'radio',
      key: 'dateClass',
      label: '班别',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      selectOptions: [
        {
          value: '白班',
          label: '白班'
        },
        {
          value: '夜班',
          label: '夜班'
        }
      ]
    },

    {
      type: 'textarea',
      key: 'remark',
      label: '备注',
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      textareaRows: 1,
      span: 24
    }
  ],
  bottomList: [
    {
      type: 'input',
      key: 'qa',
      label: 'IPQC审核',
      disabled: true,
      span: 6
    },
    {
      type: 'input',
      key: 'foreman',
      label: '领班审核',
      disabled: true,
      span: 6
    },
    {
      type: 'input',
      key: 'examiner',
      label: '主管审核',
      disabled: true,
      span: 6
    },
    {
      type: 'input',
      key: 'operator',
      label: '首/自主',
      defaultValue: `首${authStore.userInfo.user.nickName || authStore.userInfo.user.userName}`,
      disabled: true,
      span: 6
    }
  ],
  showIPQCExamine: true,
  modalHeight: '70vh',
  modleSearchUrlObj: {
    lotId: '/rcerp/agv/erp/findByLotNo'
  },
  modalClose() {
    moduleNoSearchData.value = null;
  },
  openModal() {
    getModuleNoList();
  }
};
